﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AuctionHouse
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int _currentHighBidder;
        
        public MainWindow()
        {
   
            InitializeComponent();

            Server NewServer = new Server();
           // NewServer.RunServer();
            // Lets get the first thread, this is the GUI Thread
            var GUIthread = Thread.CurrentThread;

            _currentHighBidder = 0; // Reset
            PopulateStack("A",80, "howdy lest try this", "#FFDFD991");
            PopulateStack("B", 120, "Freakshow again", "#ABDFD991");
            PopulateStack("C", 210, "I am a very fine painting", "#ABDFD911");
            PopulateStack("D", 130, "another day at the office", "#ABDFD191");
            PopulateStack("E", 99, "another day at the office", "#ABDFD191");
            PopulateStack("F", 66, "another day at the office", "#ABDFD191");
            PopulateStack("G", 110, "another day at the office", "#ABDFD191");
            PopulateStack("H", 130, "A LOUIS XIV ORMOLU-MOUNTED AND BRASS-INLAID FAUX-TORTOISESHELL CLOCK ON PEDESTALCIRCA 1710 THE CLOCK AND PEDESTAL PROBABLY ASSOCIATED", "#ABDFD191");

            // Populate Auction List with available Auctions
            PopulateItemMenus(cbAuctionSelector, "Ming Vases Auction Fall - 2013");
            PopulateItemMenus(cbAuctionSelector, "20'ies Watches Auction Fall - 2013");
            PopulateItemMenus(cbAuctionSelector, "Impressionists Auction Fall - 2013");
            cbAuctionSelector.SelectedIndex = 1;


            var duration = new Duration(TimeSpan.FromSeconds(10));
            var doubleanimation = new DoubleAnimation(100.0, duration);
            
            //pb.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
            // Should be able to use a Find here, FindWhereName
           /* foreach (ucUserObject uc in spUserStack.Children.OfType<ucUserObject>().Where(uc => uc.Name == "A"))
            {
                uc.pbTimeLeft.BeginAnimation(RangeBase.ValueProperty, doubleanimation);
            }*/
           
            // Animate the long bar accross the window
            pbAuctionBar.BeginAnimation( RangeBase.ValueProperty, new DoubleAnimation(100.0, new Duration(TimeSpan.FromSeconds(30))));

        }

        private void PopulateItemMenus(ComboBox cb, string itemText)
        {
            if (cb == null) throw new ArgumentNullException("cb");
            var cbox = cb as ComboBox;
            var newitem = new ComboBoxItem {Content = itemText};
            cb.Items.Add(newitem);

        }


        private void PopulateStack(string name, int bid, string description, string hexa)
        {
            if (bid >_currentHighBidder)
            {
                _currentHighBidder = bid;
                lbLotHighBid.Content = _currentHighBidder.ToString(CultureInfo.InvariantCulture);
            }
            
            var aUserObject = new ucUserObject
            {
                lbUserName = {Content = Environment.UserName.ToString(CultureInfo.InvariantCulture)},
                lbBid = {Content = bid.ToString(CultureInfo.InvariantCulture)},
                tbDescription = {Text = description},
                Name = name
            };

            var col = (Color)ColorConverter.ConvertFromString(hexa);
            aUserObject.rColourRect.Fill = new SolidColorBrush(col);

            spUserStack.Children.Add(aUserObject);
        }
    }
}
    